<?php
class TgiachidoansController extends AppController {

	var $name = 'Tgiachidoans';
	var $uses = array('Tgiachidoan','Chidoansv','Khoahoc','Thang','Doanphisv','Tgdongdoanphisv','Khoahoc','Namhoc','Hocky'); 

	function index($khoahoc=null, $chidoan=null,$namhoc=null,$hocky = null) {
		$khoahocs = $this->Khoahoc->find('list',array('fields'=>array('nambatdau'),'order'=>array('id DESC')));
		if(!$khoahoc)
			$khoahoc = key($khoahocs);
		$chidoans = $this->Chidoansv->find('list',array('fields'=>array('chidoan'),'conditions'=>array('khoahoc_id'=>$khoahoc),'order'=>array('id DESC')));
		$namhocs = $this->Namhoc->find('list',array('fields'=>array('namhoc'),'order'=>array('id DESC')));
		if(!$khoahoc)
			$namhoc = key($namhoc);
		$hockys = $this->Hocky->find('list',array('fields'=>array('tenhocky'),'conditions'=>array('namhoc_id'=>$namhoc)));
		$this->set(compact('khoahocs','chidoans','namhocs','hockys'));
		$hk = $this->Hocky->find('first',array('conditions'=>array('Hocky.id'=>$hocky)));
		if(!empty($hk))
		{
			$this->Tgiachidoan->recursive = 0;
			if($chidoan)
			{			
				$this->set('tgiachidoans', $this->paginate(array('ngaychuyen !='=> null,'Chidoansv.id'=>$chidoan,'Chidoansv.khoa_id'=>$this->Session->read('khoa_id'),'ngaychuyen >='=>$hk['Hocky']['ngaybatdau'],'ngaychuyen <='=>$hk['Hocky']['ngayketthuc'])));
			}
			else
			{
				$this->set('tgiachidoans', $this->paginate(array('ngaychuyen !='=> null,'Chidoansv.khoahoc_id'=>$khoahoc,'Chidoansv.khoa_id'=>$this->Session->read('khoa_id'),'ngaychuyen >='=>$hk['Hocky']['ngaybatdau'],'ngaychuyen <='=>$hk['Hocky']['ngayketthuc'])));
			}
		}
		$this->set(compact('khoahoc','chidoan','namhoc','hocky'));
	}
    
    function thanhtich() {
		$this->Tgiachidoan->recursive = 0;
		$this->set('tgiachidoans', $this->paginate(array('chidoansv_id'=>$this->Session->read('chidoanht_id'))));
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid tgiachidoan', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('tgiachidoan', $this->Tgiachidoan->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Tgiachidoan->create();
			if ($this->Tgiachidoan->save($this->data)) {
				$this->Session->setFlash(__('The tgiachidoan has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The tgiachidoan could not be saved. Please, try again.', true));
			}
		}
		$chidoansvs = $this->Tgiachidoan->Chidoansv->find('list');
		$sinhviens = $this->Tgiachidoan->Sinhvien->find('list');
		$this->set(compact('chidoansvs', 'sinhviens'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Không tồn tại', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			$ngaychuyen = ($this->data['Tgiachidoan']['ngaychuyenn']==''?NULL:date('Y-m-d',strtotime($this->data['Tgiachidoan']['ngaychuyenn'])));
			$this->Tgiachidoan->id=$this->data['Tgiachidoan']['id'];
			if ($this->Tgiachidoan->saveField('ngaychuyen', $ngaychuyen))
			{
				$this->Session->setFlash(__('Thông tin đã lưu', true),'success');
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('Thông tin không thể lưu', true),'error');
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Tgiachidoan->read(null, $id);
		}
		$this->set(compact('khoahocs','chidoansvs', 'sinhviens'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for tgiachidoan', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Tgiachidoan->delete($id)) {
			$this->Session->setFlash(__('Tgiachidoan deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Tgiachidoan was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}

	function chuyensinhhoat()
	{
		if(!empty($this->data))
		{
			$err=0;
			foreach ($this->data['Tgiachidoan']['check'] as $key =>$value)
			{
				if($value==1)
				{
					if(!$this->Tgiachidoan->updateAll(array('ngaychuyen'=>"'".date('Y-m-d',strtotime($this->data['Tgiachidoan']['ngaychuyen'][$key]))."'",'ghichu'=>"'".$this->data['Tgiachidoan']['ghichu'][$key]."'",'noichuyen'=>"'".$this->data['Tgiachidoan']['noichuyen'][$key]."'"),array('Tgiachidoan.id'=>$key)))
						$err=1;
				}
			}
			if($err==1)
			{
				$this->Session->setFlash('Thực thi không thành công');
			}
			else
			{
				$this->Session->setFlash('Thực thi thành công');
			}
			$this->redirect(array('action'=>'chuyensinhhoat'));
		}
		$khoahocs = $this->Khoahoc->find('list',array('fields'=>array('nambatdau'),
														'order'=>' id DESC'));
		$khoahoc = key($khoahocs);
		$khoa_id = $this->Session->read('khoa_id');
		$querystring= "select Tgiachidoan.chidoansv_id, sl as soluong
    					from tgiachidoans as Tgiachidoan, (select a.chidoansv_id, count(sinhvien_id) as sl from tgiachidoans a where ngaychuyen is NULL group by a.chidoansv_id) as Tgiachidoan1
    					where Tgiachidoan.chidoansv_id = Tgiachidoan1.chidoansv_id and Tgiachidoan1.sl>0 and ngaychuyen is NULL
    						group by Tgiachidoan.chidoansv_id";
    	$laycd_act = $this->Tgiachidoan->query($querystring);
    	
    	$cd_id = array();
    	$soluong = array();
    	foreach ($laycd_act as $cd) 
    	{
    		$cd_id[] = $cd['Tgiachidoan']['chidoansv_id'];
    	}    
		//hien thi sinhvien can cu vao $sv 
		$this->Chidoansv->displayField='tenchidoan' ;
    	$chidoans = $this->Chidoansv->find('list',array('conditions'=>array('khoa_id'=>$khoa_id,'khoahoc_id'=>$khoahoc,'id'=>$cd_id)));
    	$this->set(compact('chidoans','khoahocs'));
	}

	function updatechidoan()
	{
		$this->layout='ajax';
		$khoahoc=$this->data['Tgiachidoan']['khoahoc_id'];
		$khoa_id = $this->Session->read('khoa_id');
		$querystring= "select Tgiachidoan.chidoansv_id, sl as soluong
    					from tgiachidoans as Tgiachidoan, (select a.chidoansv_id, count(sinhvien_id) as sl from tgiachidoans a where ngaychuyen is NULL group by a.chidoansv_id) as Tgiachidoan1
    					where Tgiachidoan.chidoansv_id = Tgiachidoan1.chidoansv_id and Tgiachidoan1.sl>0 and ngaychuyen is NULL
    						group by Tgiachidoan.chidoansv_id";
    	$laycd_act = $this->Tgiachidoan->query($querystring);
    	
    	$cd_id = array();
    	$soluong = array();
    	foreach ($laycd_act as $cd) 
    	{
    		$cd_id[] = $cd['Tgiachidoan']['chidoansv_id'];
    	}    
		//hien thi sinhvien can cu vao $sv 
		$this->Chidoansv->displayField='chidoan' ;
    	$chidoans = $this->Chidoansv->find('list',array('conditions'=>array('khoa_id'=>$khoa_id,'khoahoc_id'=>$khoahoc,'id'=>$cd_id)));
    	$this->set(compact('chidoans'));
	}

	function updatedoanvien()
	{
		$this->layout='ajax';
		$chidoan = $this->data['Tgiachidoan']['chidoan_id'];
		$dv_ids = $this->Tgiachidoan->find('list',array('conditions'=>array('chidoansv_id'=>$chidoan,'ngaychuyen'=>null),'fields'=>array('sinhvien_id')));
		$doanphis =array();
		foreach ($dv_ids as $id)
		{
			$nv = $this->Tgiachidoan->find('list',array('conditions'=>array('sinhvien_id'=>$id,'ngaychuyen'=>NULL),'fields'=>array('sinhvien_id','ngayvao')));
			if(isset($nv[$id]))
			{
				$thangd = date('n-Y',strtotime($nv[$id]));
				$thang_idd = $this->Thang->find('list',array('conditions'=>array('Thang.thang'=>$thangd),'fields'=>array('thang','id')));
				$thangkt = date('n-Y');
				$thang_idkt = $this->Thang->find('list',array('conditions'=>array('Thang.thang'=>$thangkt),'fields'=>array('thang','id')));
				if(isset($thang_idd[$thangd]) && isset($thang_idkt[$thangkt]))
				{
					$tien =  $this->Doanphisv->find('all',array('conditions'=>array('thang_id >='=>$thang_idd[$thangd],'thang_id <='=>$thang_idkt[$thangkt]),'fields'=>array('sum(sotien) as sotien')));
				}
				$joins =array(array('table' => 'doanphisvs',
            						'type' => 'INNER',
            						'alias' => 'Doanphisv',
            						'conditions' => array(
                					'Tgdongdoanphisv.thang_id = Doanphisv.thang_id')));
				$tiendpdong = $this->Tgdongdoanphisv->find('all',array('joins'=>$joins,'conditions'=>array('sinhvien_id'=>$id,'Tgdongdoanphisv.chidoansv_id'=>$chidoan),'fields'=>('sum(Doanphisv.sotien) as sotien')));
				$tiebphaidong = isset($tien[0][0]['sotien'])?$tien[0][0]['sotien']:0;
				$tiendadong = isset($tiendpdong[0][0]['sotien'])?$tiendpdong[0][0]['sotien']:0;
				$doanphis[$id]= ($tiebphaidong==''?0:$tiebphaidong) -($tiendadong==''?0:$tiendadong);
			}
		}
		
		$doanviens = $this->Tgiachidoan->find('all',array('conditions'=>array('chidoansv_id'=>$chidoan,'ngaychuyen'=>null)));	
		$this->set(compact('doanviens','doanphis','chidoan'));
	}

	function capnhatnoidung()
	{
		$this->layout='ajax';
		$hocky = $this->data['Tgiachidoan']['hocky'];
		$hk = $this->Hocky->find('first',array('conditions'=>array('Hocky.id'=>$hocky)));
		$namhoc = $this->data['Tgiachidoan']['namhoc'];
		if(!empty($hk))
		{
			$khoahoc = $this->data['Tgiachidoan']['khoahoc_id'];
			$chidoan = $this->data['Tgiachidoan']['chidoan_id'];
			$this->Tgiachidoan->recursive = 0;
			if(!empty($this->data['Tgiachidoan']['chidoan_id']))
			{			
				$this->set('tgiachidoans', $this->paginate(array('ngaychuyen !='=> null,'Chidoansv.id'=>$chidoan,'Chidoansv.khoa_id'=>$this->Session->read('khoa_id'),'ngaychuyen >='=>$hk['Hocky']['ngaybatdau'],'ngaychuyen <='=>$hk['Hocky']['ngayketthuc'])));
			}
			else
			{
				$chidoan = 0;
				$this->set('tgiachidoans', $this->paginate(array('ngaychuyen !='=> null,'Chidoansv.khoahoc_id'=>$khoahoc,'Chidoansv.khoa_id'=>$this->Session->read('khoa_id'),'ngaychuyen >='=>$hk['Hocky']['ngaybatdau'],'ngaychuyen <='=>$hk['Hocky']['ngayketthuc'])));
			}
			$this->set(compact('khoahoc','chidoan','hocky','namhoc'));
		}
	} 

	function capnhathocky()
	{
		$this->layout='ajax';
		$namhoc = $this->data['Tgiachidoan']['namhoc'];
		$hockys = $this->Hocky->find('list',array('fields'=>array('tenhocky'),'conditions'=>array('namhoc_id'=>$namhoc)));
		$this->set(compact('hockys'));
	}
}
